// File Name: rs485_init.c
// Author: kukukukuku~
// Date: 2024-12-20

#include "apm32f00x_gpio.h"
#include "apm32f00x_misc.h"
#include "apm32f00x_usart.h"
#include "./RS485/inc/rs485_init.h"
#include "./RS485/inc/rs485_macros.h"

/**
 * @brief   RS485 GPIO配置
 * @param   None
 * @retval  None
 * @note    静态
*/
static void rs485_gpio_config(void)
{
    GPIO_Config_T gpioConfig = 
    {
        .pin   = RS485_MODE_GPIO_PIN,
        .mode  = GPIO_MODE_OUT_PP,
        .speed = GPIO_SPEED_10MHz,
        .intEn = GPIO_EINT_DISABLE,
    };
    
    GPIO_Config(RS485_MODE_GPIO_PORT, &gpioConfig);
}


/**
 * @brief   RS485 USART配置
 * @param   None
 * @retval  None
 * @note    静态
*/
static void rs485_usart_config(void)
{
    USART_Config_T usartConfig = 
    {
        .baudRate   = RS485_USART_BAUDRATE,
        .mode       = RS485_USART_MODE,
        .interrupt  = USART_INT_NONE,
        .parity     = RS485_USART_PARITY_BIT,
        .stopBits   = RS485_USART_STOP_BIT,
        .wordLength = RS485_USART_DATA_BIT,
    };
    
    USART_Config(RS485_USART, &usartConfig);
    USART_ClearStatusFlag(RS485_USART, USART_FLAG_RXBNE);
    USART_EnableInterrupt(RS485_USART, USART_INT_RX);
    USART_EnableInterrupt(RS485_USART, USART_INT_IDLE);
    NVIC_EnableIRQRequest(RS485_USART_RX_IRQ, 0);
    USART_Enable(RS485_USART);
}


/**
 * @brief   RS485初始化
 * @param   None
 * @retval  None
 * @note    None
*/
void rs485_init(void)
{
    rs485_gpio_config();
    rs485_usart_config();
    
    RS485_SET_MODE_TO_RECEIVE;
}
